package com.yunwei.http;

import android.test.InstrumentationTestCase;

import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.OkHttpClient;
import com.yunwei.model.LoginUser;

import java.io.IOException;
import java.util.List;

import retrofit.BaseUrl;
import retrofit.Call;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;

/**
 * @author by weijianxing on 10/7/15.
 */
public class UserServiceTest extends InstrumentationTestCase {

    UserService userService;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.networkInterceptors().add(new MyOkHttpInterceptor());

        BaseUrl baseUrl = new BaseUrl() {
            @Override
            public HttpUrl url() {
                return new HttpUrl.Builder().scheme("http").host("192.168.1.9").addPathSegment("/api/").port(8081).build();
            }
        };

        String url = "http://192.168.1.9:8081/api/";
        Retrofit retrofit = new Retrofit.Builder().baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create()).client(okHttpClient).build();

        userService = retrofit.create(UserService.class);

    }

    public void testListUsers() throws IOException {
        Call<List<LoginUser>> userList = userService.listUsers();
        List<LoginUser> body = userList.execute().body();
        for (LoginUser user : body) {
            System.out.printf(user.getId().toString());
        }
    }


}
